<?php

App::uses('AppController', 'Controller');

/**
 * Teachers Controller
 *
 * @property Teacher $Teacher
 */
class TeachersController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->paginate = array('Teacher' => array('order' => array('Teacher.name' => 'ASC')));
        $this->Teacher->recursive = 0;

        $this->set('teachers', $this->paginate());
    }

    public function beforeFilter() {
        $this->isAuthorized('admin');
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        $this->Teacher->id = $id;
        if (!$this->Teacher->exists()) {
            throw new NotFoundException(__('Invalid teacher'));
        }
        $this->set('teacher', $this->Teacher->read(null, $id));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        $this->loadModel('User');
        if ($this->request->is('post')) {
            $this->Teacher->create();
            $this->User->create();
            $user  = explode(' ',$this->request->data['Teacher']['name'] );
            $data = array(
                'User' => array(
                    'username' => $user[0].'.'.$user[sizeof($user)-1],
                    'password' => $user[0],
                    'role' => 'teacher',
                    'active' => 1,
                )
            );
            /*if ($this->request->data['Teacher']['cpf'] == '') {
                $this->Session->setFlash(__('The cpf is necessary.'));
                $this->redirect(array('action' => 'index'));
            }*/

            if ($this->User->validateMany($data)) {
                $this->User->save($data);
                $this->request->data['Teacher']['userfk'] = $this->User->getId();
                if ($this->Teacher->save($this->request->data)) {
                    $this->Session->setFlash(__('The teacher has been saved'));
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The teacher could not be saved. Please, try again.'));
                }
            } else {
                $this->Session->setFlash(__('This cpf already exists on the database.'));
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        $this->Teacher->id = $id;
        if (!$this->Teacher->exists()) {
            throw new NotFoundException(__('Invalid teacher'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Teacher->save($this->request->data)) {
                $this->Session->setFlash(__('The teacher has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The teacher could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Teacher->read(null, $id);
        }
    }

    /**
     * delete method
     *
     * @throws MethodNotAllowedException
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->loadModel('User');
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Teacher->id = $id;
        $teacher = $this->Teacher->read(null, $id);
        $this->User->id = $teacher['Teacher']['userfk'];
        if (!$this->Teacher->exists()) {
            throw new NotFoundException(__('Invalid teacher'));
        }
        if ($this->Teacher->delete()) {
            $this->User->delete();
            $this->Session->setFlash(__('Teacher deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Teacher was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

    public function managerWorkLoad() {
        if ($this->request->is('post')) {

            $data = array();
            foreach ($this->request->data as $teacher => $works) {
                $data[] = array(
                    'Teacher' => array(
                        'workload' => $works[0],
                        'additional_workload' => $works[1],
                        'id' => $teacher
                    )
                );
            }

            if ($this->Teacher->saveMany($data)) {
                $this->Session->setFlash(__('The workload\'s has been saved'));
            }
            else
                $this->Session->setFlash(__('The workload\'s could not be saved. Please, try again.'));
        }
        $this->set('teachers', $this->Teacher->find('all', array('order' => array('Teacher.name' => 'ASC'))));
    }

}
